//
//  MU_ZoomingScrollView.m
//  NanKaiNews
//
//  Created by QianK on 12-12-19.
//
//

#import "MU_ZoomingScrollView.h"
//#import  <SDWebImage/UIImageView+WebCache.h>
//#import  <SDWebImage/UIImageView+WebCache.h>
#import "UIImageView+WebCache.h"
#import "XDHeader.h"

@implementation MU_ZoomingScrollView
@synthesize imageName = _imageName;
@synthesize imageView = _imageView;

#define MAX_ZOOMSCALE 3.0
#define MIN_ZOOMSCALE 0.8


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.delegate = self;
		self.minimumZoomScale = 0.8;
		self.maximumZoomScale = 3.0;
		self.showsVerticalScrollIndicator = NO;
		self.showsHorizontalScrollIndicator = NO;
		
		imageView  = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
		imageView.contentMode = UIViewContentModeScaleAspectFit;//保持图片原大小
//        imageView.contentMode = UIViewContentModeScaleToFill;
		[self addSubview:imageView];
    }
    return self;
}


-(UIImageView*)imageView
{
    return imageView;
}

-(void)setImageName:(NSString *)imageName
{
    if (![_imageName isEqualToString:imageName]) {
        _imageName = imageName;
        
        NSString * imageUrl = [NSString stringWithFormat:@"%@%@",ImageMainUrl,_imageName];
        NSString * urlstring = [imageUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//        imageView.tag = 200;
        imageView.backgroundColor = UIColorFromRGB(0xdbdada);
        [imageView setImageWithURL:[NSURL URLWithString:urlstring] placeholderImage:[UIImage imageNamed:@"placehold_image"]];
        
//        [imageView setImageWithURL:[NSURL URLWithString:imageName] placeholderImage:dPic_imagePlaceHolder];
//        [imageView setImageWithURL:[NSURL URLWithString:imageName] placeholderImage:nil];
    }
}


#pragma mark - scrollview delegate
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
	return imageView;
}

- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale
{
	CGFloat zs = scrollView.zoomScale;
	zs = MAX(zs, 1.0);//能缩小，但是自动返回原状
	zs = MIN(zs, MAX_ZOOMSCALE);
	
	[UIView beginAnimations:nil context:NULL];
	[UIView setAnimationDuration:0.02];//返回原状的时间
	scrollView.zoomScale = zs;
	[UIView commitAnimations];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
	//NSLog(@"%s", _cmd);
	
	UITouch *touch = [touches anyObject];
	
	if ([touch tapCount] == 2)
	{
		//双击
		CGFloat zs = self.zoomScale;
		zs = (zs == 1.0) ? 2.0 : 1.0;//双击放大的倍数=2倍
		
		[UIView beginAnimations:nil context:NULL];
		[UIView setAnimationDuration:0.3];
		self.zoomScale = zs;
		[UIView commitAnimations];
	}
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
